// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System.Collections.Generic;

namespace SnapObjects.Data.SqlServer
{
    internal class SqlServerSqlSelectStatementGenerator : SqlSelectStatementGenerator
    {
        private readonly string _rowNumberName = "__P_P_H_S__ROW_NUMBER__P_P_H_E__";

        public SqlServerSqlSelectStatementGenerator(
            ISqlStatementFactory factory,
            ISqlGenerationHelper helper,
            ITypeMapper typeMapper) : base(factory, helper, typeMapper)
        {
        }

        protected override void CreateSelectKeyword(List<SqlToken> tokens, SqlSelectStatement select)
        {
            tokens.Add(new SqlKeywordToken(SqlKeywords.SELECT));

            if (select.IsDistinst)
            {
                tokens.Add(SqlWhitespaceToken.Whitespace);
                tokens.Add(new SqlKeywordToken(SqlKeywords.DISTINCT));
            }

            if (select.Take > -1)
            {
                tokens.Add(SqlWhitespaceToken.Whitespace);
                tokens.Add(new SqlKeywordToken(SqlServerSqlKeywords.TOP));
                tokens.Add(new SqlCharacterToken(SqlCharacters.OpenParentheses));
                tokens.Add(new SqlNumericToken(select.Take.ToString()));
                tokens.Add(new SqlCharacterToken(SqlCharacters.CloseParentheses));

                if (select.Skip > -1)
                {
                    if (base.CreateSelectColumns(tokens, select))
                    {
                        tokens.Add(SqlWhitespaceToken.Whitespace);
                        tokens.Add(new SqlKeywordToken(SqlKeywords.FROM));
                        tokens.Add(new SqlCharacterToken(SqlCharacters.OpenParentheses));

                        base.CreateSelectKeyword(tokens, select);
                    }
                }
            }
            else
            {
                tokens.Add(SqlWhitespaceToken.Whitespace);
            }
        }

        protected override bool CreateSelectColumns(List<SqlToken> tokens, SqlSelectStatement select)
        {
            if (base.CreateSelectColumns(tokens, select))
            {
                if (select.Skip > -1)
                {
                    tokens.Add(new SqlCharacterToken(SqlCharacters.Comma));
                    tokens.Add(SqlWhitespaceToken.Whitespace);
                    tokens.Add(new SqlFunctionToken("ROW_NUMBER()"));
                    tokens.Add(SqlWhitespaceToken.Whitespace);
                    tokens.Add(new SqlKeywordToken(SqlServerSqlKeywords.OVER));
                    tokens.Add(new SqlCharacterToken(SqlCharacters.OpenParentheses));

                    if (select.OrderBy.Count > 0)
                    {
                        this.CreateOrderBy(tokens, select);
                    }
                    else
                    {
                        tokens.Add(new SqlKeywordToken(SqlKeywords.ORDER));
                        tokens.Add(SqlWhitespaceToken.Whitespace);
                        tokens.Add(new SqlKeywordToken(SqlKeywords.BY));
                        tokens.Add(new SqlCharacterToken(SqlCharacters.OpenParentheses));
                        tokens.Add(new SqlKeywordToken(SqlKeywords.SELECT));
                        tokens.Add(SqlWhitespaceToken.Whitespace);
                        tokens.Add(new SqlNumericToken("1"));
                        tokens.Add(new SqlCharacterToken(SqlCharacters.CloseParentheses));
                    }

                    tokens.Add(new SqlCharacterToken(SqlCharacters.CloseParentheses));
                    tokens.Add(SqlWhitespaceToken.Whitespace);
                    tokens.Add(new SqlKeywordToken(SqlKeywords.AS));
                    tokens.Add(SqlWhitespaceToken.Whitespace);
                    tokens.Add(new SqlOtherToken(_rowNumberName));
                }

                return true;
            }

            return false;
        }

        protected override bool CreateWhere(List<SqlToken> tokens, SqlSelectStatement select)
        {
            var result = base.CreateWhere(tokens, select);

            if (select.Skip > -1)
            {
                tokens.Add(SqlWhitespaceToken.Whitespace);
                if (result)
                {
                    tokens.Add(new SqlKeywordToken(SqlKeywords.AND));
                }
                else
                {
                    tokens.Add(new SqlKeywordToken(SqlKeywords.WHERE));
                }

                tokens.Add(SqlWhitespaceToken.Whitespace);
                tokens.Add(new SqlOtherToken(_rowNumberName));
                tokens.Add(new SqlOperatorToken(SqlOperator.GreaterThan));
                tokens.Add(new SqlNumericToken(select.Skip.ToString()));
            }

            return result;
        }

        protected override void CreateTerminator(List<SqlToken> tokens, SqlSelectStatement select)
        {
            if (select.Skip > -1)
            {
                tokens.Add(new SqlCharacterToken(SqlCharacters.CloseParentheses));
                tokens.Add(SqlWhitespaceToken.Whitespace);
                tokens.Add(new SqlKeywordToken(SqlKeywords.AS));
                tokens.Add(SqlWhitespaceToken.Whitespace);
                tokens.Add(new SqlOtherToken("__TABLE_NAME__"));
            }
        }
    }
}
